// pages/commodity_list/commodity_list.js
var app = getApp();
let goodsList = [
  { actEndTime: '2019/06/20 10:00:43' },
]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    index: 0,
    hidden: false,
    // 价格高低
    jiage: "aes",
    countDownList: [],
    actEndTimeList: []
  },
  // 导航栏内容排列
  changelist: function (e) {
    var that = this;
    var index = e.currentTarget.id
    console.log(index)
    // 点击切换
    if (index == 1) {
      if (this.data.hidden == false) {
        this.setData({
          hidden: true
        })
      } else {
        this.setData({
          hidden: false
        })
      }
    }
    this.setData({
      index: index
    })
    // 最新
    if (index == 0) {
      that.shuju()
    }
    // 按照价格排序
    if (index == 1) {
      var jiagepailie = e.currentTarget.dataset.id
      if (jiagepailie != 'desc') {
        that.shuju("", 'money', 'desc')
        this.setData({
          jiage: 'desc'
        })
        console.log(this.data.jiage)
      } else {
        that.shuju("", 'money', 'asc')
        this.setData({
          jiage: 'asc'
        })
        console.log(this.data.jiage)
      }
    }
    // 按照销量
    if (index == 2) {
      that.shuju("", 'salNum', 'desc')
    }
  },
  // 详情
  detail: function (e) {
    var ticketId = e.currentTarget.dataset.id
    console.log(ticketId)
    wx.navigateTo({
      url: '../list_detail/list_detail?ticketId=' + ticketId,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 根据分享跳转页面
    console.log(options)
  },
  timeFormat(param) {//小于10的格式化函数
    return param < 10 ? '0' + param : param;
  },
  countDown() {//倒计时函数
    // 获取当前时间，同时得到活动结束时间数组
    let newTime = new Date().getTime();
    let endTimeList = this.data.actEndTimeList;
    let countDownArr = [];
    let obj = null;
    // 对结束时间进行处理渲染到页面
    endTimeList.forEach(o => {
      let endTime = new Date(o).getTime();
      // 如果活动未结束，对时间进行处理
      if (endTime - newTime > 0) {
        let time = (endTime - newTime) / 1000;
        // 获取天、时、分、秒
        let day = parseInt(time / (60 * 60 * 24));
        let hou = parseInt(time % (60 * 60 * 24) / 3600);
        let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
        let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
        obj = {
          day: this.timeFormat(day),
          hou: this.timeFormat(hou),
          min: this.timeFormat(min),
          sec: this.timeFormat(sec)
        }
        countDownArr.push(obj);
      } else {//活动已结束，全部设置为'00'
        obj = {
          day: '00',
          hou: '00',
          min: '00',
          sec: '00'
        }
        countDownArr.push(obj);
      }
    })

    // 渲染，然后每隔一秒执行一次倒计时函数
    this.setData({ countDownList: countDownArr })
    // console.log(this.data.countDownList)
    setTimeout(this.countDown, 1000);
  },

  // 请求数据
  shuju: function (page, fiel, par) {
    var that = this;
    if (page == "" || page == null) {
      page = 1
    }
    if (par == "" || par == null) {
      par = ''
    }
    if (fiel == "" || fiel == null) {
      fiel = 'createTime'
    }
    wx.request({
      url: app.globalData.appletUrl + '/applet/ticketList',
      data: {
        page: 1,
        shopId: app.globalData.shopId,
        field: fiel,
        sort: par,
        prefectureId:""
      },
      success: function (res) {
        console.log(res)
        var list = res.data.data
        // 倒计时
        let endTimeList = [];
        for (var i = 0; i < list.length; i++) {
          endTimeList.push(list[i].snapTime)
        }
        that.setData({
          list: list,
          actEndTimeList: endTimeList
        })
        console.log(endTimeList)
        // 执行倒计时函数
        that.countDown();
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.shuju()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  // 下拉刷新页面
  shuju1: function (page, fiel, par) {
    var that = this;
    if (page == "" || page == null) {
      console.log(31)
      page = 1
    }
    if (par == "" || par == null) {
      par = ''
    }
    if (fiel == "" || fiel == null) {
      fiel = 'createTime'
    }
    wx.request({
      url: app.globalData.appletUrl + '/applet/ticketList',
      data: {
        page: page,
        shopId: app.globalData.shopId,
        field: fiel,
        sort: par
      },
      success: function (res) {
        console.log(res)
        var list = that.data.list.concat(res.data.data)
        // 倒计时
        let endTimeList = [];
        console.log(list[0].snapTime)
        for (var i = 0; i < list.length; i++) {
          endTimeList.push(list[i].snapTime)
        }
        that.setData({
          list: list,
          actEndTimeList: endTimeList
        })
        // 执行倒计时函数
        that.countDown();
      }
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var page = 1
    page++
    this.shuju1(page, "", "")
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this
    console.log(res)
    return {
      title: "景点列表",
      path: 'pages/list_detail/list_detail?id=' + that.data.ticketId
    }
    if (res.from == 'menu') {
      var member = wx.getStorageSync("memberId")
      wx.request({
        url: t.globalData.appletUrl + '/applet/memberShare',
        data: {
          shopId: t.globalData.shopId,
          memberId: member
        },
        header: {
          "content-type": "application/json"
        },
        success: function (res) {

        }
      })
    }
  }
})